import axios from 'axios'

import { baseURL_dev } from '../config/baseUrl'
// import { aesUtil } from './encryp/aesUtil.js'
// import { rsaUtil } from './encryp/rsaUtil.js'
import { useRouter, useRoute } from "vue-router";

const route = useRoute();
const router = useRouter();

// 初始化axios
const instance = axios.create({
    baseURL:baseURL_dev,
    timeout:30000
    // headers:{
    //     'Content-Type':'application/x-www-form-urlencoded'
    // }
})

// 请求拦截器
instance.interceptors.request.use(function (config) {
    //每次发送请求前，向请求头中添加token信息
    console.log(config)
    config.headers['token'] = sessionStorage.getItem('token')
    return config
},function(error){
    return Promise.reject(error)
})

//响应拦截器
instance.interceptors.response.use(function (response){
    return response
},function(error){
    return Promise.reject(error)
})

//下载
export const $doload = async (url:string,params:object={})=>{
    let {data} = await instance.post(url,params)
    return data
}

//get请求方法 
export const $get = async (url:string,params:object={})=>{
    let {data} = await instance.get(url,{params})
    if(data.code==6){
        sessionStorage.clear();
        router.push('/login')
    }
    return data
}

//登录 
export const $logins = async (url:string,params:object={})=>{
    let {data} = await instance.post(url,params)
    return data
}

//post请求方法 
export const $post = async (url:string,params:object={})=>{
    let {data} = await instance.post(url,params)
    if(data.code==6){
        sessionStorage.clear();
        router.push('/login')
    }
    return data
}

//del请求方法 
export const $del = async (url:string,params:object={})=>{
	let {data} = await instance.delete(url,params)
    if(data.code==6){
        sessionStorage.clear();
        router.push('/login')
    }
    return data
}

//put请求方法 
export const $put = async (url:string,params:object={})=>{
    let {data} = await instance.put(url,params)
    if(data.code==6){
        sessionStorage.clear();
        router.push('/login')
    }
    return data
}